package pers.ljf.escloud.controller.sys;

import org.springframework.web.bind.annotation.*;
import pers.ljf.escloud.common.req.DepartmentSaveReq;
import pers.ljf.escloud.dao.entity.app.Department;
import pers.ljf.escloud.dao.entity.app.User;
import pers.ljf.escloud.service.dto.Result;
import pers.ljf.escloud.service.modules.app.DepartmentService;
import pers.ljf.escloud.service.modules.app.UserService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {

    @Resource
    private DepartmentService departmentService;

    @Resource
    private UserService userService;

    @GetMapping("/list")
    public List<Department> list(@RequestParam Map<String, Object> params) {
        List<Department> departments = departmentService.list(getUserId(),params);
        return departments;
    }

    @GetMapping("/info/{deptId}")
    public Result info(@PathVariable("deptId") Long menuId){
        Department dept = departmentService.getById(menuId);
        User user = userService.getById(dept.getDirectorId());
        dept.setAdminUserName(user.getUsername());
        return Result.ok().put("dept", dept);
    }

    @PostMapping("/save")
    public Result save(@RequestBody @Valid DepartmentSaveReq req) {
        departmentService.save(req);
        return Result.ok("更新成功");
    }

    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        departmentService.removeById(id);
        return Result.ok("更新成功");
    }
}
